package com.atguigu4.search_sort.exer1;

/**
 * ClassName: ArrayExer2
 * Package: com.atguigu4.search_sort.exer2
 * Description:
 * 数组的缩容
 * 现有数组 int[] arr={1,2,3,4,5,6,7} 现需删除数组中索引为4的元素。
 *
 * @Author 姜
 * @Create 2023/11/15 10:20
 * @Version 1.0
 */
public class ArrayExer2 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};

        int deleteIndex = 4;

//        // 方式1：不新建数组
//        for (int i = deleteIndex; i < arr.length - 1; i++) {
//            arr[i] = arr[i + 1];
//        }
//        // 修改最后一个元素，设置为默认值
//        arr[arr.length - 1] = 0;


        // 方式2：新建一个数组，新数组的长度比原有数组的长度少一位
        int[] newArr = new int[arr.length - 1];
        for (int i = 0; i < deleteIndex; i++) {
            newArr[i] = arr[i];
        }

        for (int i = deleteIndex; i < arr.length - 1; i++) {
            newArr[i] = arr[i + 1];
        }

        arr = newArr;

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "\t");
        }

    }
}
